package com.yhq.basic.nio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author hqyin
 * @date 2025/6/5 9:47 AM
 */
public class NIOClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8888);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if("exit".equals(line)) {
                break;
            }
            OutputStream out = socket.getOutputStream();
            out.write(("client:"+line).getBytes(StandardCharsets.UTF_8));

            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            if(-1!=inputStream.read(bytes)) {
                String msg = new String(bytes);
                System.out.println(msg.trim());
            }
        }


    }
}
